/*
 * Copyright 2016. chenshufei
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package sz.szsmk.citizencard.util;

import android.content.SharedPreferences;
import android.util.Base64;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import sz.szsmk.citizencard.DaggerApplication;


/**
 * 获取安全的SharePreferences，采用RSA 加salt及向量的 对key与value一起加密的SharePreferences
 * <br /> author: chenshufei
 * <br /> date: 15/10/9
 * <br /> email: chenshufei2@sina.com
 */
public class SecureSharedPreferences {
    
    //安全的SharePreferences，采用RSA 加salt及向量的 对key与value一起加密的SharePreferences
    private static SecurePreferences securePrefs;
    static {
        securePrefs = new SecurePreferences(DaggerApplication.getIns(), "", "my_pref");
        SecurePreferences.setLoggingEnabled(true);
    }

    public static void putString(String key, String value){
        SharedPreferences.Editor editor = securePrefs.edit();
        editor.putString(key, value);
        editor.commit();
    }

    public static void putInt(String key, int value){
        SharedPreferences.Editor editor = securePrefs.edit();
        editor.putInt(key, value);
        editor.commit();
    }

    public static void putBoolean(String key, boolean value){
        SharedPreferences.Editor editor = securePrefs.edit();
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static int getInt(String key){
        return securePrefs.getInt(key, 0);
    }

    public static int getInt(String key, int default_value){
        return securePrefs.getInt(key,default_value);
    }

    public static String getString(String key){
        return securePrefs.getString(key, "");
    }

    public static String getString(String key, String default_value){
        return securePrefs.getString(key, default_value);
    }

    public static boolean getBoolean(String key, boolean defValue){
        return securePrefs.getBoolean(key, defValue);
    }

    public static void putSerObject( String key, Object saveObject) {
        SharedPreferences.Editor editor = securePrefs.edit();
        String string = Object2String(saveObject);
        editor.putString(key, string);
        editor.commit();
    }
    public static Object getSerObject( String key) {

        String string = securePrefs.getString(key, null);
        if (string != null) {
            Object object = String2Object(string);
            return object;
        } else {
            return null;
        }
    }
    private static String Object2String(Object object) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = null;
        try {
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(object);
            String string = new String(android.util.Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
            objectOutputStream.close();
            return string;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 使用Base64解密String，返回Object对象
     *
     * @param objectString 待解密的String
     * @return object      解密后的object
     */
    private static Object String2Object(String objectString) {
        byte[] mobileBytes = Base64.decode(objectString.getBytes(), Base64.DEFAULT);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);
        ObjectInputStream objectInputStream = null;
        try {
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Object object = objectInputStream.readObject();
            objectInputStream.close();
            return object;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
